package com.chen.pfm.provider;

import com.chen.pfm.ResourceTable;
import com.chen.pfm.entity.ReportForms;
import ohos.aafwk.ability.AbilitySlice;
import ohos.agp.components.*;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
import java.util.List;

public class ReportFormProvider extends BaseItemProvider {
    static final HiLogLabel LABEL = new HiLogLabel(HiLog.LOG_APP, 0x2022001, "ReportFormProvider");
    private List<ReportForms> list;
    private AbilitySlice slice;

    public ReportFormProvider(List<ReportForms> list, AbilitySlice slice) {
        this.list = list;
        this.slice = slice;
    }

    @Override
    public int getCount() {
        return list == null ? 0 : list.size();
    }

    @Override
    public Object getItem(int i) {
        if (list != null && i >= 0 && i < list.size()) {
            return list.get(i);
        }
        return null;
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    class ReportFormHolder {
        Text title;
        Text number;

        public ReportFormHolder(Component component) {
            this.title = (Text) component.findComponentById(ResourceTable.Id_item_title);
            this.number = (Text) component.findComponentById(ResourceTable.Id_item_number);
        }
    }

    @Override
    public Component getComponent(int i, Component component, ComponentContainer componentContainer) {
        final Component cpt;
        ReportFormHolder holder = null;
        ReportForms forms = list.get(i);
        if (component == null) {
            cpt = LayoutScatter.getInstance(slice).parse(ResourceTable.Layout_template_items_report_forms, null, false);
            holder = new ReportFormHolder(cpt);
            cpt.setTag(holder);
        } else {
            cpt = component;
            holder = (ReportFormHolder) cpt.getTag();
        }
        holder.title.setText(forms.getName());
        holder.number.setText(String.valueOf(forms.getN()));

        return cpt;
    }


}
